package pacman.state;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import pacman.base.Score;
import pacman.util.ScoreRecord;
import pacman.util.ScoreTableLoader;

public class LevelLose extends BasicGameState {

    public static final int ID = 3;
    private StateBasedGame sbg;
    private GameContainer gc;
    private String playerName;
    
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		this.sbg = sbg;
		this.gc = gc;
		playerName = new String();
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.drawString("You Lose! !", 100, 100);
		g.drawString("Please enter your name!", 100, 150);
		g.setColor(Color.red);
		g.drawString(playerName, 100, 300);
		g.setColor(Color.white);
		g.drawString("Press enter to write your name into scoretable!", 100, 450);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	
    }
    
    public void keyReleased(int key, char c) {
		if (key == Input.KEY_ENTER) {
		    writeScore();
		    sbg.enterState(ScoreTable.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
		}
		if (key == Input.KEY_BACK) {
		    if (playerName.length() > 0) {
			playerName = playerName.substring(0, playerName.length() - 1);
		    }
		}
		if (playerName.length() < 20) {
			if (key == Input.KEY_A) playerName = playerName + "A";
		    if (key == Input.KEY_B) playerName = playerName + "B";
		    if (key == Input.KEY_C) playerName = playerName + "C";
		    if (key == Input.KEY_D) playerName = playerName + "D";
		    if (key == Input.KEY_E) playerName = playerName + "E";
		    if (key == Input.KEY_F) playerName = playerName + "F";
		    if (key == Input.KEY_G) playerName = playerName + "G";
		    if (key == Input.KEY_H) playerName = playerName + "H";
		    if (key == Input.KEY_I) playerName = playerName + "I";
		    if (key == Input.KEY_J) playerName = playerName + "J";
		    if (key == Input.KEY_K) playerName = playerName + "K";
		    if (key == Input.KEY_L) playerName = playerName + "L";
		    if (key == Input.KEY_M) playerName = playerName + "M";
		    if (key == Input.KEY_N) playerName = playerName + "N";
		    if (key == Input.KEY_O) playerName = playerName + "O";
		    if (key == Input.KEY_P) playerName = playerName + "P";
		    if (key == Input.KEY_Q) playerName = playerName + "Q";
		    if (key == Input.KEY_R) playerName = playerName + "R";
		    if (key == Input.KEY_S) playerName = playerName + "S";
		    if (key == Input.KEY_T) playerName = playerName + "T";
		    if (key == Input.KEY_U) playerName = playerName + "U";
		    if (key == Input.KEY_V) playerName = playerName + "V";
		    if (key == Input.KEY_W) playerName = playerName + "W";
		    if (key == Input.KEY_X) playerName = playerName + "X";
		    if (key == Input.KEY_Y) playerName = playerName + "Y";
		    if (key == Input.KEY_Z) playerName = playerName + "Z";
		    if (key == Input.KEY_SPACE) playerName = playerName + " ";
		}
		if (key == Input.KEY_F2) {
		    try {
				if (!gc.isFullscreen()) gc.setFullscreen(true);
				else gc.setFullscreen(false);
		    } catch (Exception e) {
		    	System.out.println(e.getMessage());
	    	}	
		}
    }

    private void writeScore() {
		try {
		    ScoreTableLoader stl = new ScoreTableLoader("resources/scoretable.properties");
		    ArrayList<ScoreRecord> scores = stl.loadScoreTable();
		    ScoreRecord sr = new ScoreRecord(playerName, Score.getFinalScore());
		    scores.add(sr);
		    stl.saveScoreTable(scores);
		    stl = null;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
    }
    
    @Override
    public int getID() {
    	return ID;
    }
}
